class Queue:
    def __init__(self, data=None):
        self.data = data
        self.next = None
        self.prev = None


class List:
    def __init__(self, max_length):
        self.head = Queue()
        self.tail = Queue()
        self.length = 0
        self.max_length = max_length

        self.tail.prev = self.head
        self.head.next = self.tail

    def is_empty(self):
        return self.length == 0

    def is_full(self):
        return self.length == self.max_length

    def alllist(self):
        tmp = self.head.next
        while tmp is not None:
            yield tmp.data
            tmp = tmp.next


# 队列、是否空、是否满
    def unshift(self, data):
# assert 断言
        assert not self.is_full()

        tmp = self.tail
        newlist = Queue()
        newlist.data = data

        nextlist = tmp
        prevlist = tmp.prev

        prevlist.next = newlist
        newlist.prev = prevlist

        nextlist.prev = newlist
        newlist.next = nextlist
        self.length += 1

    def shift(self):
        assert not self.is_empty()
        nowlist = self.head.next
        nextlist = nowlist.next

        self.head.next = nextlist
        nextlist.prev = self.head
        self.length -= 1

        return nowlist.data

    def __iter__(self):
        node = self.head.next
        while node != self.tail:
            yield node.data
            node = node.next


if __name__ == '__main__':
    l= List(10)

    for x in range(0, 10):
        l.unshift(x)

    for i in l:
        print(i)

    print('======shift=======')
    l.shift()
    for i in l:
        print(i)

    print('======unshift=======')
    try:
        print('length is : %d' %(l.length))
        l.unshift(100)
    except Exception:
        print('full')
        l.shift()
        l.unshift(100)
    for i in l:
        print(i)

